package com.ls.lserp.modules.produce.service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.DateUtils;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.modules.order.entity.OperatingP;
import com.ls.lserp.modules.order.entity.StyleOptionEntity;
import com.ls.lserp.modules.produce.dao.ProducePlanDao;
import com.ls.lserp.modules.produce.dao.ProduceWorkticketDao;
import com.ls.lserp.modules.produce.entity.ProducePlan;
import com.ls.lserp.modules.produce.entity.ProduceWorkticketEntity;
import com.ls.lserp.modules.produce.entity.WorkticketEntity;
import com.ls.lserp.modules.purchase.entity.PurchaseContract;
import com.ls.lserp.modules.sys.entity.Sequence;
import com.ls.lserp.modules.sys.entity.User;
import com.ls.lserp.modules.sys.utils.SequenceUtil;
@Service
@Transactional(readOnly = false)
public class ProduceWorkticketService extends CrudService<ProduceWorkticketDao,ProduceWorkticketEntity>{

	@Autowired
    private ProduceWorkticketDao produceWorkticketDao;
	
	/**
     * 得到班组信息
     */
    public List<ProduceWorkticketEntity> getGroupList() {
    	return produceWorkticketDao.findGroupList();
    }
    
    /**
     * 得到工序信息
     */
    public List<ProduceWorkticketEntity> getOperatingList() {
    	return produceWorkticketDao.findOperatingList();
    }
    
    /**
     * 得到工票信息
     */
    public Page<ProduceWorkticketEntity> getProduceWorkticketList(Page<ProduceWorkticketEntity> page, ProduceWorkticketEntity produceWorkticketEntity) {
    	// 设置分页参数
    	produceWorkticketEntity.setPage(page);
    	List<ProduceWorkticketEntity> pList = produceWorkticketDao.findList(produceWorkticketEntity);
    	if (pList != null && pList.size()>0) {
    		produceWorkticketEntity.setUpdateBy(new User());
			for (int i = 0; i < pList.size(); i++) {
				produceWorkticketEntity.getUpdateBy().setId(pList.get(i).getUpdateBy().getId());
				String numOfPackage = produceWorkticketDao.getNumOfPkg(produceWorkticketEntity);
				pList.get(i).setNumOfPackage(numOfPackage);
			}
		}
        // 执行分页查询
        page.setList(pList);
        return page;
    }
    
    /**
     * 通过订单号得到工票信息
     */
    public Page<ProduceWorkticketEntity> getProduceWorkticketListByOrder(Page<ProduceWorkticketEntity> page, ProduceWorkticketEntity produceWorkticketEntity) {
    	// 设置分页参数
    	produceWorkticketEntity.setPage(page);
        // 执行分页查询
        page.setList(produceWorkticketDao.findListByOrder(produceWorkticketEntity));
        return page;
    }
    
    /**
     * 制作单耗list供导出用
     * @param produceWorkticketEntity
     * @return
     * @throws ParseException 
     */
    public List<WorkticketEntity> makeOptionList(WorkticketEntity workticketEntity) throws ParseException{
    	List<WorkticketEntity> workticketList = produceWorkticketDao.findProduceWorkticketList(workticketEntity);
    	BigDecimal countNum = new BigDecimal(0); 
    	if (workticketList != null && workticketList.size() > 0) {
			for (WorkticketEntity wEntity : workticketList) {
				wEntity.setProcessNumber((new BigDecimal(wEntity.getProcessNumber())).setScale(0).toString());
				if (workticketEntity.getUserGroupEntity() != null) {
					if (workticketEntity.getUserGroupEntity().getUserGroupId() == 31) {
						//扣眼
						if (wEntity.getOperatingPEntity().getItemId() == 16) {
							wEntity.setItemNameCn("54.扣眼");
							if (wEntity.getRemarks().indexOf("Different_Color_Thread") != -1){
								wEntity.setUnitPrice(ResourcesUtil.getConfig("buttonhole_notWhite"));
								countNum = new BigDecimal(wEntity.getProcessNumber()).multiply(new BigDecimal(wEntity.getUnitPrice()));
								wEntity.setAmount(countNum.setScale(2).toString());
							}else {
								wEntity.setUnitPrice(ResourcesUtil.getConfig("buttonhole_white"));
								countNum = new BigDecimal(wEntity.getProcessNumber()).multiply(new BigDecimal(wEntity.getUnitPrice()));
								wEntity.setAmount(countNum.setScale(2).toString());
							} 
						//手缝全身
						} else if (wEntity.getOperatingPEntity().getItemId() == 90) {
							wEntity.setUnitPrice(ResourcesUtil.getConfig("hand_Sewing_WholeBody"));
							countNum = new BigDecimal(wEntity.getProcessNumber()).multiply(new BigDecimal(wEntity.getUnitPrice()));
							wEntity.setAmount(countNum.setScale(2).toString());
						//订扣
						} else if (wEntity.getOperatingPEntity().getItemId() == 91) {
							if (wEntity.getRemarks().indexOf("Different_Color_Thread") != -1){
								wEntity.setUnitPrice(ResourcesUtil.getConfig("fastener_notWhite"));
								countNum = new BigDecimal(wEntity.getProcessNumber()).multiply(new BigDecimal(wEntity.getUnitPrice()));
								wEntity.setAmount(countNum.setScale(2).toString());
							}else {
								wEntity.setUnitPrice(ResourcesUtil.getConfig("fastener_white"));
								countNum = new BigDecimal(wEntity.getProcessNumber()).multiply(new BigDecimal(wEntity.getUnitPrice()));
								wEntity.setAmount(countNum.setScale(2).toString());
							}
						//暗排打结
						} else if (wEntity.getOperatingPEntity().getItemId() == 92) {
							if (wEntity.getRemarks().indexOf("Different_Color_Thread") != -1){
								wEntity.setUnitPrice(ResourcesUtil.getConfig("darkRow_Tie_notWhite"));
								countNum = new BigDecimal(wEntity.getProcessNumber()).multiply(new BigDecimal(wEntity.getUnitPrice()));
								wEntity.setAmount(countNum.setScale(2).toString());
							}else {
								wEntity.setUnitPrice(ResourcesUtil.getConfig("darkRow_Tie_white"));
								countNum = new BigDecimal(wEntity.getProcessNumber()).multiply(new BigDecimal(wEntity.getUnitPrice()));
								wEntity.setAmount(countNum.setScale(2).toString());
							}
						//AMF收口
						} else if (wEntity.getOperatingPEntity().getItemId() == 93) {
							wEntity.setUnitPrice(ResourcesUtil.getConfig("amf_Close"));
							countNum = new BigDecimal(wEntity.getProcessNumber()).multiply(new BigDecimal(wEntity.getUnitPrice()));
							wEntity.setAmount(countNum.setScale(2).toString());
						//手缝商标
						} else if (wEntity.getOperatingPEntity().getItemId() == 95) {
							if (wEntity.getRemarks().indexOf("Different_Color_Thread") != -1){
								wEntity.setUnitPrice(ResourcesUtil.getConfig("hand_Trademark_notWhite"));
								countNum = new BigDecimal(wEntity.getProcessNumber()).multiply(new BigDecimal(wEntity.getUnitPrice()));
								wEntity.setAmount(countNum.setScale(2).toString());
							}else {
								wEntity.setUnitPrice(ResourcesUtil.getConfig("hand_Trademark_white"));
								countNum = new BigDecimal(wEntity.getProcessNumber()).multiply(new BigDecimal(wEntity.getUnitPrice()));
								wEntity.setAmount(countNum.setScale(2).toString());
							}
						//剪线毛（订扣）
						} else if (wEntity.getOperatingPEntity().getItemId() == 96) {
							wEntity.setUnitPrice(ResourcesUtil.getConfig("fastener_Cut_Thrum"));
							countNum = new BigDecimal(wEntity.getProcessNumber()).multiply(new BigDecimal(wEntity.getUnitPrice()));
							wEntity.setAmount(countNum.setScale(2).toString());
						}
					}
				}
				if (wEntity.getRemarks() != null) {
					//白线
					if ("White_Thread".equals(wEntity.getRemarks())) {
						wEntity.setRemarks("");
						//异色线
					}else if (wEntity.getRemarks().indexOf("Different_Color_Thread") != -1){
						wEntity.setRemarks(wEntity.getRemarks().replace("Different_Color_Thread", "异色线"));
					}
					//袖头包含HMBU(两眼一扣缠高脖)
					if (wEntity.getRemarks().indexOf("HMBU") != -1) {
						wEntity.setRemarks(wEntity.getRemarks().replace("HMBU", "两眼一扣缠高脖"));
					}
				}
			}
		}
		return workticketList;
    }
    
    /**
     * 通过用户ID获取工票明细
     * @param produceWorkticketEntity
     * @return
     * @throws ParseException
     */
    public List<ProduceWorkticketEntity> findDetailList(ProduceWorkticketEntity produceWorkticketEntity) throws ParseException{
		return produceWorkticketDao.findDetailList(produceWorkticketEntity);
    }
    
}
